package com.xian.spark.jdbc;

import org.apache.spark.SparkConf;
import org.apache.spark.SparkContext;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.DataFrameReader;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.hive.HiveContext;

import java.util.HashMap;

/**
 * spark 1.6.x 版本
 */
public class ReadMysql {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf();
        conf.setMaster("local");
        conf.setAppName("ReadMysql");

        JavaSparkContext jsc = new JavaSparkContext(conf);

        HiveContext hiveContext = new HiveContext(jsc);

        DataFrameReader read = hiveContext.read();
        HashMap<String, String> options = new HashMap<>();
        options.put("driver","com.mysql.jdbc.Driver");
        options.put("url","jdbc:mysql://r71:3306/testdb?characterEncoding=utf8&useSSL=TRUE");
        options.put("user","root");
        options.put("password","123456");
        options.put("dbtable","person");





        Dataset<Row> tableDS = read.format("jdbc").options(options).load();

        tableDS.printSchema();
        //tableDS.show(); //默认显示20行
        tableDS.show(10000); //字段显示N行

        jsc.stop();
        jsc.close();



    }
}
